package com.gome.bg.entityDao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.transaction.IllegalTransactionStateException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import com.danga.MemCached.MemCachedClient;
import com.gome.bg.entityDao.RbToAtgDao;
import com.gome.datasource.DynamicDataSource;
import com.gome.datasource.MongoTemplate;
import com.gome.util.JetmInfo;
import com.gome.util.TraceInfo;
import com.googlecode.ehcache.annotations.Cacheable;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

public class RbToAtgDaoImpl implements RbToAtgDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	
	@Autowired
	private JmsTemplate jmsTemplate;
	
	public Destination getDestination() {
		return destination;
	}

	public void setDestination(Destination destination) {
		this.destination = destination;
	}

	@Autowired
	private Destination destination;
	

	@Autowired
	private MemCachedClient memcachedClient;

	@Autowired
	private MongoTemplate mongoTemplate;

	@Autowired
	private DataSourceTransactionManager transactionManager;

	@Autowired
	private DefaultTransactionDefinition def;
	
	
	//@Transactional(readOnly = true)//只读属性 
	@Cacheable(cacheName = "baseCache")
	public List ErgodicWithRefundNoForRefundBean(Map map) {

		JetmInfo.FLAG = true;
		JetmInfo.JETMStart("   mongoTemplate test  ");
		TransactionStatus status = transactionManager.getTransaction(def);
		DynamicDataSource.setCurrentLookupKey((String) map.get("ambient"));
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String refundNo = (String) map.get("refundNo");
		String sql = "select * from tbl_refund_bank_task where refund_no='" + refundNo + "' ";
		TraceInfo.trace(getClass()).debug(sql);
		System.out.println(jdbcTemplate.queryForList(sql));
		final String message=sql;
		jmsTemplate.send(destination, new MessageCreator() {  
            public Message createMessage(Session session) throws JMSException {  
                return session.createTextMessage(message);  
            }  
        });  
        System.out.println("成功发送了一条JMS消息"); 
		
		try{
		   list = jdbcTemplate.queryForList(sql);
		}catch(Exception e){
			TraceInfo.trace(getClass()).error(e.toString());
		}
		System.out.println(list);
		TraceInfo.trace(getClass()).info(list.toString());
		memcachedClient.add("list", list);
		try {
			try {
				transactionManager.commit(status);
			} catch (Exception e) {
				TraceInfo.trace(getClass()).error("事务提交异常");
				
			}
		} catch (Exception ex) {
			TraceInfo.trace(getClass()).error("出现事务异常");
			try {
				transactionManager.rollback(status);
			} catch (IllegalTransactionStateException e) {
				TraceInfo.trace(getClass()).error("回滚数据异常");
			}
		}
		JetmInfo.JETMCollect();
		JetmInfo.JETMEnd();
		return list;
	}

	@Cacheable(cacheName = "baseCache")
	public List ErgodicWithRefundNoForLog(Map map) {
		DynamicDataSource.setCurrentLookupKey((String) map.get("ambient"));
		String refundNo = (String) map.get("refundNo");
		String sql = "select * from tbl_refund_bank_log where refund_no='" + refundNo + "' ";
		TraceInfo.traceDebug(RbToAtgDaoImpl.class, sql);
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	
	
	@Cacheable(cacheName = "baseCache")
	public List ErgodicWithRefundNoAndDetailId(Map map) {
		DynamicDataSource.setCurrentLookupKey((String) map.get("ambient"));
		String refundNo = (String) map.get("refundNo");
		String refundDetailId = (String) map.get("refundDetailId");
		String sql = "select * from tbl_refund_bank_task where refund_no='" + refundNo + "' and refund_Details_Id='" + refundDetailId + "' ";
		TraceInfo.traceDebug(RbToAtgDaoImpl.class, sql);
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	
	
	//只读关闭，遇到DataAccessException异常回滚！如果不对异常进行处理，该异常将一直向上层抛出，直至抛出到页面！ 
	//@Transactional(readOnly = false, rollbackFor = DataAccessException.class) 
	public int SearchFroRefundNoWithUpdate(Map map) {
//		JetmInfo.FLAG = true;
//		JetmInfo.JETMStart("   mongoTemplate test  ");
//		try {
//			DBObject empObj = new BasicDBObject();
//			empObj.put("uname", "teddy");
//			empObj.put("upwd", "psw");
//			// 添加
//			mongoTemplate.add(empObj);
//			mongoTemplate.query();
//			DBObject empObj1 = new BasicDBObject();
//			empObj1.put("uname", "teddyf");
//			empObj1.put("upwd", "password");
//			// 添加
//			mongoTemplate.add(empObj1);
//
//			DBObject updateObj = new BasicDBObject();
//			updateObj.put("uname", "teddy");
//			updateObj.put("upwd", "mmm");
//			// 更新
//			mongoTemplate.modify(new BasicDBObject("uname", "teddy"), updateObj);
//			TraceInfo.traceInfo(RbToAtgDaoImpl.class, "-----------------------修改后-------------------");
//			mongoTemplate.query();
//
//			// 删除
//			mongoTemplate.delete(new BasicDBObject("uname", "teddyf"));
//			TraceInfo.traceInfo(RbToAtgDaoImpl.class, "-----------------------删除后-------------------");
//			mongoTemplate.query();
//		} catch (Exception e) {
//			TraceInfo.trace(getClass()).error(e.toString());
//		}
//		JetmInfo.JETMCollect();
//		JetmInfo.JETMStart("   SearchFroRefundNoWithUpdate  ");
//		TransactionStatus status = transactionManager.getTransaction(def);
//		DynamicDataSource.setCurrentLookupKey((String) map.get("ambient"));
//		String refundNo = (String) map.get("refundNo");
//		String sql = " update tbl_refund_bank_task set refund_state ='P' where refund_no='" + refundNo + "' ";
//		TraceInfo.trace(getClass()).debug(sql);
//		System.out.println("1234567890");
//		int i = 0;
//		
//		try {
//			i= jdbcTemplate.update(sql);
//		
//			try {
//				transactionManager.commit(status);
//			} catch (Exception e) {
//				TraceInfo.traceError(RbToAtgDaoImpl.class, "事务提交异常");
//			}
//		} catch (Exception ex) {
//			TraceInfo.traceError(RbToAtgDaoImpl.class, "出现事务异常");
//			try {
//				transactionManager.rollback(status);
//			} catch (IllegalTransactionStateException e) {
//				TraceInfo.traceError(RbToAtgDaoImpl.class, "回滚数据异常");
//			}
//		}
//		JetmInfo.JETMCollect();
//		JetmInfo.JETMEnd();
//		return i;
		return 0;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public DataSourceTransactionManager getTransactionManager() {
		return transactionManager;
	}

	public void setTransactionManager(DataSourceTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}

	public DefaultTransactionDefinition getDef() {
		return def;
	}

	public void setDef(DefaultTransactionDefinition def) {
		this.def = def;
	}

	public MemCachedClient getMemcachedClient() {
		return memcachedClient;
	}

	public void setMemcachedClient(MemCachedClient memcachedClient) {
		this.memcachedClient = memcachedClient;
	}

	public MongoTemplate getMongoTemplate() {
		return mongoTemplate;
	}

	public void setMongoTemplate(MongoTemplate mongoTemplate) {
		this.mongoTemplate = mongoTemplate;
	}

	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}
	
	
	
}
